﻿using System;
using System.IO.Ports;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using Tools;

namespace BLL
{
    public class PortBLL
    {
        private UIBLL uIBLL = new UIBLL();
        public PortBLL(TextBox textBoxRec,Label label)
        {
            this.textBoxRec = textBoxRec;
            this.label = label;
        }
        private TextBox textBoxRec;
        private Label label;
        private delegate void invokehandler();
        public void OpenPort(SerialPort serialPort, string port,int baudRate)
        {
            try
            {
                serialPort.PortName = port;
                serialPort.BaudRate = baudRate;
                serialPort.Parity = Parity.None;
                serialPort.DataBits = 8;
                serialPort.StopBits = StopBits.One;
                if (!serialPort.IsOpen)
                {
                    serialPort.Open();
                }
            }
            catch (Exception)
            {
                MessageBox.Show("打开串口失败！");
            }

        }
        public string SendData(SerialPort port, string data)
        {
            string reData = "";
            data = data + "\r\n";
            //AppHelper.Delay(50);
            port.WriteLine(data);
            LogHelper.WritePortLog(port.PortName+":" +data);
            //DateTime time1 = DateTime.Now;
            while (true)
            {
                //DateTime time2 = DateTime.Now;
                AppHelper.Delay(50);
                if (port.BytesToRead > 1)
                {
                    AppHelper.Delay(50);
                    byte[] recData = new byte[port.BytesToRead];
                    port.Read(recData, 0, recData.Length);
                    reData = Encoding.Default.GetString(recData).Replace("?","").Trim();
                    LogHelper.WritePortLog(port.PortName + ":" + reData);
                    if (reData.ToLower()=="recieveok")
                    {
                        port.WriteLine(data);
                        LogHelper.WritePortLog(port.PortName + ":" + data);
                        uIBLL.LabelShowMsg(label,port.PortName + ":通信失败!尝试重新通信!");
                        continue;
                    }
                    //if (reData.Contains("openok"))
                    //{
                    //    AppHelper.Delay(260);
                    //}
                    uIBLL.TextBoxShowMsg(textBoxRec, port.PortName +":"+ reData.ToLower());
                    return reData.ToLower();
                }
                //if (time2 > time1.AddMinutes(1))
                //{
                //    string da = GetData(port);
                //}
            }
        }
        public string GetData(SerialPort port)
        {
            string reData = "";
            while (true)
            {
                AppHelper.Delay(50);
                if (port.BytesToRead > 1)
                {
                    AppHelper.Delay(50);
                    byte[] recData = new byte[port.BytesToRead];
                    port.Read(recData, 0, recData.Length);
                    reData = Encoding.Default.GetString(recData);
                    LogHelper.WritePortLog(port.PortName + ":" + reData);
                    uIBLL.TextBoxShowMsg(textBoxRec, port.PortName + ":" + reData.ToLower());
                    return reData.ToLower();
                }
            }
        }
    }
}
